package com.atguigu.spring5.proxy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author : wenchao.long
 * @date : Created in 2020/10/29 16:45 周四
 * @description :
 */
@Component
@Aspect
@Order(0)
public class UserProxy {

    /**
     * 相同切入点抽取
     */
    @Pointcut("execution(* com.atguigu.spring5.bean.User.add(..))")
    public void pointcut() {

    }

    /**
     * 前置通知
     */
    @Before("pointcut()")
    public void before() {
        System.out.println("before...");
    }

    /**
     * 不管有没有异常都会通知
     * 最终通知
     */
    @After("pointcut()")
    public void after() {
        System.out.println("after...");
    }

    /**
     * 有异常不执行
     * 后置通知(返回通知)
     */
    @AfterReturning("pointcut()")
    public void afterReturning() {
        System.out.println("afterReturning...");
    }

    @AfterThrowing("pointcut()")
    public void afterThrowing() {
        System.out.println("afterThrowing...");
    }

    /**
     * 环绕通知
     * @param pjp pjp
     * @return Object
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around...之前");
        pjp.proceed();
        System.out.println("around...之后");
        return pjp;
    }
}
